<script setup>

import {computed, ref} from "vue";
import BranchNode from "./base/BranchNode.vue";
import nodeMixin from "../NodeMixin.js";

const props = defineProps({
  ...nodeMixin.props,
  moveLn: Boolean,
  moveRn: Boolean,
  isDefault: Boolean
})
const emit = defineEmits([...nodeMixin.emits, 'moveL', 'moveR'])
const _value = computed(nodeMixin.computed._value(props, emit))

const showErr = ref(false)
const errInfo = ref(null)

defineExpose({ validate })

const content = computed(() => {
  if (props.isDefault) {
    return '其他情况将进入此分支'
  }else {
    let desc = '请设置条件'
    const prop = _value.value.props
    if (prop.groups.length > 0 && prop.groups[0].conditions.length > 0){
      desc = prop.groups.map(group => {
        if ((group.conditions || []).length === 0){
          return '请添加条件'
        }
        return group.conditions.map(cd => {
          switch (cd.type) {
            //case ''
          }
          return '?'
        }).join(` ${group.logic ? '且' : '或'} `)
      }).join(` ${prop.logic ? '且' : '或'} `)
    }
    return desc
  }
})

function validate(err){

}
</script>

<template>
  <branch-node v-model="_value" :readonly="readonly" :show-error="showErr" :error-info="errInfo"
                color="#59B9A4" header-icon="share" :is-default="isDefault"
               :content="content" :moveRn="moveRn" :moveLn="moveLn"
               @insertNode="type => emit('insertNode', type)" :desc="`优先级 ${index + 1}`"
               @move-l="emit('moveL')" @move-r="emit('moveR')"
               @delete="emit('delete')" @select="emit('select', modelValue)"/>
</template>

<style scoped>

</style>
